package csl.bluetooth.tools.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 陈少磊
 * @version 1.0.0
 * @ClassName MD5Util.java
 * @Description TODO
 * @createTime 2021年03月25日 12:25:00
 */
public class MD5Util {
    public static String MD5(String plainString) {

        if(plainString != null) {
            try {
                //创建具有指定算法名称的信息摘要
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                messageDigest.update(plainString.getBytes());
                //使用指定的字节数组对摘要进行最后更新，然后完成摘要计算
                byte[] byteData = messageDigest.digest();

                StringBuilder hexString = new StringBuilder();
                for (byte aByteData : byteData) {
                    String hex = Integer.toHexString(0xff & aByteData);
                    if (hex.length() == 1) {
                        hexString.append('0');
                    }
                    hexString.append(hex);
                }
                return hexString.toString();
            } catch (NoSuchAlgorithmException e) {/* log */}
        }

        return "";
    }
}
